Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relayer/Contract: Move decimal conversion from relayer code to contract code. #54

Merged
merged 16 commits into from
Dec 1, 2023

Conversation

keyleu
Copy link
Collaborator

@keyleu keyleu commented Nov 29, 2023

Description

  • Move amount conversion logic from Relayer to Contract.
  • Add checks for MAX and MIN sending precision, and not allowing higher precision than decimals for Coreum registration tokens.

Reviewers checklist:

  • Try to write more meaningful comments with clear actions to be taken.
  • Nit-picking should be unblocking. Focus on core issues.

Authors checklist

  • Provide a concise and meaningful description
  • Review the code yourself first, before making the PR.
  • Annotate your PR in places that require explanation.
  • Think and try to split the PR to smaller PR if it is big.

This change is Reviewable

@keyleu keyleu requested a review from a team as a code owner November 29, 2023 18:22
@keyleu keyleu requested review from dzmitryhil, miladz68, ysv and wojtek-coreum and removed request for a team November 29, 2023 18:23
Copy link
Contributor

@dzmitryhil dzmitryhil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 11 files reviewed, 10 unresolved discussions (waiting on @keyleu, @miladz68, @wojtek-coreum, and @ysv)


contract/src/contract.rs line 238 at r1 (raw file):

    if sending_precision > decimals.try_into().unwrap() {
        return Err(ContractError::TokenSendingPrecisionTooHigh {});
    }

Those 2 functions should be moved to separate function and used for both XRPL and coreum tokens. Since now you don't validate the sending_precision <= decimals for the XRPL token, but should.

Code quote:

    if !(MIN_SENDING_PRECISION..=MAX_SENDING_PRECISION).contains(&sending_precision) {
        return Err(ContractError::InvalidSendingPrecision {});
    }

    if sending_precision > decimals.try_into().unwrap() {
        return Err(ContractError::TokenSendingPrecisionTooHigh {});
    }

contract/src/contract.rs line 797 at r1 (raw file):

            to_json_binary(&query_coreum_tokens(deps, offset, limit)?)
        }
        QueryMsg::CoreumTokenByXRPLCurrency { xrpl_currency } => {

We don't need that query anymore.


integration-tests/coreum/contract_client_test.go line 1105 at r1 (raw file):

	issueFee := chains.Coreum.QueryAssetFTParams(ctx, t).IssueFee

	// TODO(dzmitryhil) Change amount in Evidence from sdkmath.Int to a different type to allow sending bigger amounts

The sdkmath.Int is the max value supported by the cosmos SDK. I guess your issue here is the way you create it.

So use the function to do the sdk.Int creation, and return back please previous amounts we used in the test. integrationtests.ConvertStringWithDecimalsToSDKInt(t, "99", 11) = 9900000000000


relayer/processes/xrpl_tx_observer.go line 132 at r1 (raw file):

	// for the coreum originated tokens we need to use toke decimals, but for the XRPL they are static
	if o.cfg.BridgeXRPLAddress.String() == deliveredXRPLAmount.Issuer.String() {
		coreumToken, err := o.contractClient.GetCoreumTokenByXRPLCurrency(ctx, stringCurrency)

Remvoe please GetCoreumTokenByXRPLCurrency form model.go/ContractClient and regenerate mocks make mockgen. Also remove it from the coreum/contract.go since we don't need it anymore.


relayer/processes/xrpl_tx_observer.go line 136 at r1 (raw file):

			return errors.Wrapf(err, "faild to get XRPL token for the XRPL to coreum transfer")
		}
		coreumAmount, err = ConvertCoreumOriginatedTokenXRPLAmountToCoreumAmount(*deliveredXRPLAmount, coreumToken.Decimals)

Remvoe the ConvertCoreumOriginatedTokenXRPLAmountToCoreumAmount and ConvertCoreumOriginatedTokenCoreumAmountToXRPLAmount functions and corresponding unit tests.


relayer/processes/xrpl_tx_observer_test.go line 155 at r1 (raw file):

						Issuer:    bridgeXRPLAddress.String(),
						Currency:  stringCurrency,
						Amount:    sdkmath.NewIntWithDecimal(999, int(15)),

Use XRPLIssuedCurrencyDecimals inseated of 15


relayer/processes/xrpl_tx_submitter_test.go line 199 at r1 (raw file):

		},
		{
			name: "register_signature_for_coreum_to_XRPL_XRPL_originated_token_transfer_payment_tx",

register_signature_for_coreum_to_XRPL_XRPL_originated_token_transfer_payment_tx -> register_signature_for_coreum_to_XRPL_token_transfer_payment_tx

(and remove test-data function it is not needed anymore )


relayer/processes/xrpl_tx_submitter_test.go line 224 at r1 (raw file):

		},
		{
			name: "submit_coreum_to_XRPL_XRPL_originated_token_transfer_payment_tx_with_filtered_signature",

submit_coreum_to_XRPL_XRPL_originated_token_transfer_payment_tx_with_filtered_signature -> submit_coreum_to_XRPL_token_transfer_payment_tx_with_filtered_signature

Also do the corresponding renaming for variables and function, since it tearms of the code now we don't have any difference between the XRPL or coreum token.

// XRPLOriginated   -> remove comment 
  
coreumToXRPLXRPLOriginatedTokenTransferOperation,    -> rename 
    coreumToXRPLXRPLOriginatedTokenTransferOperationWithSignatures,    -> rename 
    coreumToXRPLXRPLOriginatedTokenTransferOperationValidSigners :=   buildCoreumToXRPLXRPLOriginatedTokenTransferTestData(t, xrplTxSigners, bridgeXRPLAddress, contractRelayers)  -> rename 

relayer/processes/xrpl_tx_submitter_test.go line 252 at r1 (raw file):

		},
		{
			name: "register_signature_for_coreum_to_XRPL_Coreum_originated_token_transfer_payment_tx",

You don't need this use case anymore since it is duplicated now, remove it.


relayer/processes/xrpl_tx_submitter_test.go line 277 at r1 (raw file):

		},
		{
			name: "submit_coreum_to_XRPL_Coreum_originated_token_transfer_payment_tx_with_filtered_signature",

You don't need this use case anymore since it is duplicated now, remove it.

Copy link
Collaborator Author

@keyleu keyleu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 17 files reviewed, 10 unresolved discussions (waiting on @dzmitryhil, @miladz68, @wojtek-coreum, and @ysv)


contract/src/contract.rs line 238 at r1 (raw file):

Previously, dzmitryhil (Dzmitry Hil) wrote…

Those 2 functions should be moved to separate function and used for both XRPL and coreum tokens. Since now you don't validate the sending_precision <= decimals for the XRPL token, but should.

Done.


contract/src/contract.rs line 797 at r1 (raw file):

Previously, dzmitryhil (Dzmitry Hil) wrote…

We don't need that query anymore.

Removed.


integration-tests/coreum/contract_client_test.go line 1105 at r1 (raw file):

Previously, dzmitryhil (Dzmitry Hil) wrote…

The sdkmath.Int is the max value supported by the cosmos SDK. I guess your issue here is the way you create it.

So use the function to do the sdk.Int creation, and return back please previous amounts we used in the test. integrationtests.ConvertStringWithDecimalsToSDKInt(t, "99", 11) = 9900000000000

Understood, was confused because newInt only returns uint64 but this one can return up to 256.

Changed it and now it's fine. thx


relayer/processes/xrpl_tx_observer.go line 132 at r1 (raw file):

Previously, dzmitryhil (Dzmitry Hil) wrote…

Remvoe please GetCoreumTokenByXRPLCurrency form model.go/ContractClient and regenerate mocks make mockgen. Also remove it from the coreum/contract.go since we don't need it anymore.

Done


relayer/processes/xrpl_tx_observer.go line 136 at r1 (raw file):

Previously, dzmitryhil (Dzmitry Hil) wrote…

Remvoe the ConvertCoreumOriginatedTokenXRPLAmountToCoreumAmount and ConvertCoreumOriginatedTokenCoreumAmountToXRPLAmount functions and corresponding unit tests.

Done


relayer/processes/xrpl_tx_observer_test.go line 155 at r1 (raw file):

Previously, dzmitryhil (Dzmitry Hil) wrote…

Use XRPLIssuedCurrencyDecimals inseated of 15

Changed.


relayer/processes/xrpl_tx_submitter_test.go line 199 at r1 (raw file):

Previously, dzmitryhil (Dzmitry Hil) wrote…

register_signature_for_coreum_to_XRPL_XRPL_originated_token_transfer_payment_tx -> register_signature_for_coreum_to_XRPL_token_transfer_payment_tx

(and remove test-data function it is not needed anymore )

Done.


relayer/processes/xrpl_tx_submitter_test.go line 224 at r1 (raw file):

Previously, dzmitryhil (Dzmitry Hil) wrote…

submit_coreum_to_XRPL_XRPL_originated_token_transfer_payment_tx_with_filtered_signature -> submit_coreum_to_XRPL_token_transfer_payment_tx_with_filtered_signature

Also do the corresponding renaming for variables and function, since it tearms of the code now we don't have any difference between the XRPL or coreum token.

// XRPLOriginated   -> remove comment 
  
coreumToXRPLXRPLOriginatedTokenTransferOperation,    -> rename 
    coreumToXRPLXRPLOriginatedTokenTransferOperationWithSignatures,    -> rename 
    coreumToXRPLXRPLOriginatedTokenTransferOperationValidSigners :=   buildCoreumToXRPLXRPLOriginatedTokenTransferTestData(t, xrplTxSigners, bridgeXRPLAddress, contractRelayers)  -> rename 

Unified everything.


relayer/processes/xrpl_tx_submitter_test.go line 252 at r1 (raw file):

Previously, dzmitryhil (Dzmitry Hil) wrote…

You don't need this use case anymore since it is duplicated now, remove it.

Removed.


relayer/processes/xrpl_tx_submitter_test.go line 277 at r1 (raw file):

Previously, dzmitryhil (Dzmitry Hil) wrote…

You don't need this use case anymore since it is duplicated now, remove it.

Removed.

@keyleu keyleu requested a review from dzmitryhil November 30, 2023 09:57
Copy link
Contributor

@dzmitryhil dzmitryhil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 17 files reviewed, 11 unresolved discussions (waiting on @keyleu, @miladz68, @wojtek-coreum, and @ysv)


integration-tests/coreum/contract_client_test.go line 1158 at r2 (raw file):

			sendingAmount:      integrationtests.ConvertStringWithDecimalsToSDKInt(t, "1.15567", 1),
			wantReceivedAmount: integrationtests.ConvertStringWithDecimalsToSDKInt(t, "1", 1),
			xrplSendingAmount:  sdkmath.NewInt(1_000_000_000_000_000),

sdkmath.NewIntWithDecimal ?


integration-tests/coreum/contract_client_test.go line 1166 at r2 (raw file):

			maxHoldingAmount:  highMaxHoldingAmount,
			sendingAmount:     integrationtests.ConvertStringWithDecimalsToSDKInt(t, "0.9999", 2),
			xrplSendingAmount: sdkmath.NewInt(999_900_000_000_000),

sdkmath.NewIntWithDecimal ?


integration-tests/coreum/contract_client_test.go line 1193 at r2 (raw file):

			maxHoldingAmount:  highMaxHoldingAmount,
			sendingAmount:     integrationtests.ConvertStringWithDecimalsToSDKInt(t, "99.9999", 6),
			xrplSendingAmount: sdkmath.NewInt(99_999_900_000_000_000),

sdkmath.NewIntWithDecimal ?


integration-tests/coreum/contract_client_test.go line 1947 at r2 (raw file):

	require.Equal(t, operationType.Currency, registeredCoreumOriginatedToken1.XRPLCurrency)
	// XRPL DECIMALS (15) - TOKEN DECIMALS (5) = 10
	require.Equal(t, operationType.Amount, amountToSendOfToken1.Mul(sdk.NewInt(10_000_000_000)))

sdkmath.NewIntWithDecimal ?


integration-tests/coreum/contract_client_test.go line 1997 at r2 (raw file):

	require.Equal(t, operationType.Currency, registeredCoreumOriginatedToken2.XRPLCurrency)
	// XRPL DECIMALS (15) - TOKEN DECIMALS (6) = 9
	require.Equal(t, operationType.Amount, amountToSendOfToken2.Mul(sdk.NewInt(1_000_000_000)))

sdkmath.NewIntWithDecimal ?


relayer/processes/xrpl_tx_observer.go line 136 at r1 (raw file):

Previously, keyleu (Keyne) wrote…

Done

Still see ConvertCoreumOriginatedTokenCoreumAmountToXRPLAmount


relayer/processes/xrpl_tx_observer.go line 129 at r2 (raw file):

	stringCurrency := xrpl.ConvertCurrencyToString(deliveredXRPLAmount.Currency)

	coreumAmount, err := ConvertXRPLOriginatedTokenXRPLAmountToCoreumAmount(*deliveredXRPLAmount)

The function name is wrong now.

  1. ConvertXRPLOriginatedTokenXRPLAmountToCoreumAmount -> ConvertXRPLAmountToCoreumAmount
  2. ConvertXRPLOriginatedTokenCoreumAmountToXRPLAmount -> ConvertXRPLCoreumAmountToXRPLAmount

And test names for it as well


relayer/processes/xrpl_tx_submitter_test.go line 199 at r1 (raw file):

Previously, keyleu (Keyne) wrote…

Done.

Still see it.


relayer/processes/xrpl_tx_submitter_test.go line 224 at r1 (raw file):

Previously, keyleu (Keyne) wrote…

Unified everything.

Check please, probably you didn't push. Since I don't see changes.


relayer/processes/xrpl_tx_submitter_test.go line 252 at r1 (raw file):

Previously, keyleu (Keyne) wrote…

Removed.

Still see it.

Copy link
Collaborator Author

@keyleu keyleu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 17 files reviewed, 11 unresolved discussions (waiting on @dzmitryhil, @miladz68, @wojtek-coreum, and @ysv)


integration-tests/coreum/contract_client_test.go line 1158 at r2 (raw file):

Previously, dzmitryhil (Dzmitry Hil) wrote…

sdkmath.NewIntWithDecimal ?

Changed.


integration-tests/coreum/contract_client_test.go line 1166 at r2 (raw file):

Previously, dzmitryhil (Dzmitry Hil) wrote…

sdkmath.NewIntWithDecimal ?

Changed.


integration-tests/coreum/contract_client_test.go line 1193 at r2 (raw file):

Previously, dzmitryhil (Dzmitry Hil) wrote…

sdkmath.NewIntWithDecimal ?

Changed.


integration-tests/coreum/contract_client_test.go line 1947 at r2 (raw file):

Previously, dzmitryhil (Dzmitry Hil) wrote…

sdkmath.NewIntWithDecimal ?

Changed


integration-tests/coreum/contract_client_test.go line 1997 at r2 (raw file):

Previously, dzmitryhil (Dzmitry Hil) wrote…

sdkmath.NewIntWithDecimal ?

Changed.


relayer/processes/xrpl_tx_observer.go line 136 at r1 (raw file):

Previously, dzmitryhil (Dzmitry Hil) wrote…

Still see ConvertCoreumOriginatedTokenCoreumAmountToXRPLAmount

Right, I removed them in one place and not others. I was confused with so many different files. Removed now.


relayer/processes/xrpl_tx_observer.go line 129 at r2 (raw file):

Previously, dzmitryhil (Dzmitry Hil) wrote…

The function name is wrong now.

  1. ConvertXRPLOriginatedTokenXRPLAmountToCoreumAmount -> ConvertXRPLAmountToCoreumAmount
  2. ConvertXRPLOriginatedTokenCoreumAmountToXRPLAmount -> ConvertXRPLCoreumAmountToXRPLAmount

And test names for it as well

Done, Changed.


relayer/processes/xrpl_tx_submitter_test.go line 199 at r1 (raw file):

Previously, dzmitryhil (Dzmitry Hil) wrote…

Still see it.

It's removed.


relayer/processes/xrpl_tx_submitter_test.go line 224 at r1 (raw file):

Previously, dzmitryhil (Dzmitry Hil) wrote…

Check please, probably you didn't push. Since I don't see changes.

I unified. Check again.


relayer/processes/xrpl_tx_submitter_test.go line 252 at r1 (raw file):

Previously, dzmitryhil (Dzmitry Hil) wrote…

Still see it.

It's removed, check again please.

Copy link
Contributor

@dzmitryhil dzmitryhil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 17 files reviewed, all discussions resolved (waiting on @miladz68, @wojtek-coreum, and @ysv)

Copy link
Contributor

@miladz68 miladz68 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 5 of 11 files at r1, 8 of 11 files at r2, 4 of 4 files at r3, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @wojtek-coreum and @ysv)

@keyleu keyleu merged commit 94d5216 into master Dec 1, 2023
4 checks passed
@keyleu keyleu deleted the keyne/move-decimal-conversion branch January 10, 2024 09:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants